package com.lin.controller;

import com.lin.reponse.Result;
import com.lin.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @ClassName OssController
 * @Description TODO
 * @Author Li
 * @Date 2022/3/4 12:01
 * @Version 1.0
 **/
@Api(tags = "oss管理")
@RestController
@CrossOrigin
@RequestMapping("/oss")
public class OssController {

    @Resource
    private OssService ossService;

    @ApiOperation(value = "创建bucket")
    @GetMapping("/getOssEntity")
    public Result getOssEntity(){
        Result bucket = ossService.createBucket();
        return Result.ok().data("bucket",bucket);
    }

    @ApiOperation(value = "上传文件")
    @PostMapping("/uploadImgFile")
    public Result upload(MultipartFile file){
        String url = ossService.upload(file);
        return Result.ok().data("url",url);
    }

    @ApiOperation(value = "列举文件")
    @PostMapping("/listFile")
    public Result listFile(){
        ossService.listFile();
        return Result.ok();
    }

    @ApiOperation(value = "删除文件")
    @PostMapping("/deleteFile")
    public Result deleteFile(String fileName){
        String[] splitFile = fileName.split(".com/");
        ossService.deleteFile(splitFile[1]);
        return Result.ok();
    }
}
